package com.boot.message.mail;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 邮件业务
 * @date 2020/3/13 21:44
 **/
@Service
@SuppressWarnings("all")
public class EmailService {

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private MailProperties properties;


    public void send(Email email) throws MessagingException {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(properties.getUsername());
        helper.setTo(email.getAddress());
        helper.setSubject(email.getTitle());
        helper.setText(email.getContent(), true);
        sender.send(message);
    }

}
